const { ccclass, property } = cc._decorator;
import n from "./frameworkManager"
import l from "./NodePool"
import s from "./MathUtil"
import c from "./MatchGameConfig"
import u from "./MatchGameAudioMgr"
import MatchGameConfig from "./MatchGameConfig";
import MathUtils from "./MathUtil";

@ccclass
class PropAniCtrl {
    public rocketUseSpData = null;
    public propellerDropSpData = null;
    public propellerFlySpData = null;
    public tntBoomSpData = null;
    public lightingSpData = null;
    public squareLightSpData = null;
    public lightBallUseSpData = null;
    public onePropUsePrefab = null;
    public effectParent = null;
    public isReady = false;
    public doubleTntSpData = null;
    public doubleLightBallSpData = null;
    public tntAndRocketSpData = null;
    public propllerAndRocketSpData = null;
    public propllerAndTntSpData = null;
    public propllerOneSpData = null;
    //  p = cc._decorator, d = p.ccclass, h = (p.property,
    constructor() {
        this.rocketUseSpData = null;
        this.propellerDropSpData = null;
        this.propellerFlySpData = null;
        this.tntBoomSpData = null;
        this.lightingSpData = null;
        this.squareLightSpData = null;
        this.lightBallUseSpData = null;
        this.onePropUsePrefab = null;
        this.effectParent = null;
        this.isReady = false;
        this.doubleTntSpData = null;
        this.doubleLightBallSpData = null;
        this.tntAndRocketSpData = null;
        this.propllerAndRocketSpData = null;
        this.propllerAndTntSpData = null;
        this.propllerOneSpData = null;
    }
    static _instance;
    static getInstance(): PropAniCtrl {
        this._instance || (this._instance = new PropAniCtrl());
        return this._instance;
    };
    public ready(e) {
        this.isReady = true;
        this.effectParent = e;
    };
    public async init(): Promise<void> {
        await this.loadPropUseSpData();
        await this.loadPropUsePrefab();
        await this.loadDoublePropSpData();
        await this.loadDoublePropPrefab();
    }

    public loadPropUseSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.loadDir("spine/propUse", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("加载单个道具使用动画数据报错", o);
                    a();
                }
                i.forEach(function (t) {
                    switch (t.name) {
                        case "HuoJian_XiaoChu":
                            e.rocketUseSpData = t;
                            break;

                        case "TuoLuo_XiaoChu":
                            e.propellerFlySpData = t;
                            break;

                        case "TuoLuo_Zha":
                            e.propellerDropSpData = t;
                            break;

                        case "ZhaYaoTongZha":
                            e.tntBoomSpData = t;
                            break;

                        case "XiaoChuXian":
                            e.lightingSpData = t;
                            break;

                        case "QiZiShanDian":
                            e.squareLightSpData = t;
                            break;

                        case "QiCaiQiuXiaoChu":
                            e.lightBallUseSpData = t;
                            break;

                        case "TuoLuo":
                            e.propllerOneSpData = t;
                    }
                });
                e.rocketUseSpData && e.propellerDropSpData && e.propellerFlySpData && e.tntBoomSpData && e.lightingSpData ? t(null) : a();
            });
        });
    };
    public loadPropUsePrefab() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("prefabs/onePropUseAniPrefab", cc.Prefab, function (o, i) {
                if (o) {
                    n.error("未找到单个道具使用动画预制体", o);
                    a();
                }
                e.onePropUsePrefab = i;
                l.initPool(e.onePropUsePrefab, 0, c.MatchAniPool.OnePropUseAniPrefab);
                t(i);
            });
        });
    };
    public loadOnePropSpData(e, t, a) {
        if(!e){
            debugger
        }
        var o = "";
        e == c.PropType.Propeller ? o = "TuoLuo" : e == c.PropType.LightingBall ? o = "QiCaiQiu" : e == c.PropType.RocketHorzontall ? o = "HuoJian" : e == c.PropType.RocketVertial ? o = "HuoJian" : e == c.PropType.Tnt && (o = "ZhaYaoTong");
        cc.loader.loadRes("spine/propUse/" + o, sp.SkeletonData, function (e1, o) {
            if (e1){
                n.error("单个道具动画加载失败", e1,e, t, a )
                debugger
            }else {
                t.getComponent(sp.Skeleton).skeletonData = o;
                a && a();
            }
        });
    };
    public playOnePropAppearAni(e, t, a) {
        var o = "Appear", i = false, r = null;
        if (e == c.PropType.Propeller) o = "Appear"; else if (e == c.PropType.LightingBall) o = "Appear"; else if (e == c.PropType.RocketHorzontall) o = "Appear_heng"; else if (e == c.PropType.RocketVertial) o = "Appear_shu"; else if (e == c.PropType.Tnt) {
            o = "Appear";
            r = "idle";
            i = true;
        }
        if (t.getComponent(sp.Skeleton).skeletonData) {
            t.getComponent(sp.Skeleton).setAnimation(0, o, false);
            r && t.getComponent(sp.Skeleton).addAnimation(0, r, i);
            a && a();
        } else this.loadOnePropSpData(e, t, function () {
            t.getComponent(sp.Skeleton).setAnimation(0, o, false);
            r && t.getComponent(sp.Skeleton).addAnimation(0, r, i);
            a && a();
        });
    };
    public playOnePropShakeAni(e, t) {
        var a = "";
        if (e == c.PropType.Propeller) a = "Checked"; else if (e == c.PropType.RocketHorzontall) a = "Checked_heng"; else if (e == c.PropType.RocketVertial) a = "Checked_shu"; else {
            if (e != c.PropType.Tnt) return;
            a = "Checked";
        }
        if (t.getComponent(sp.Skeleton).skeletonData) t.getComponent(sp.Skeleton).setAnimation(0, a, true); else {
            t.getComponent(sp.Skeleton).skeletonData = null;
            this.loadOnePropSpData(e, t, function () {
                t.getComponent(sp.Skeleton).setAnimation(0, a, true);
            });
        }
    };
    public playOnePropIdleAni(e, t) {
        var a = false, o = "idle";
        if (e == c.PropType.Propeller) o = "idle"; else if (e == c.PropType.LightingBall) o = "idle"; else if (e == c.PropType.RocketHorzontall) o = "idle_heng"; else if (e == c.PropType.RocketVertial) o = "idle_shu"; else if (e == c.PropType.Tnt) {
            o = "idle";
            a = true;
        }
        t.getComponent(sp.Skeleton).skeletonData ? t.getComponent(sp.Skeleton).setAnimation(0, o, a) : this.loadOnePropSpData(e, t, function () {
            t.getComponent(sp.Skeleton).setAnimation(0, o, a);
        });
    };
    public playOnePropTouchAni() { };
    public createOnePropUseItem(e, t, a) {
        var o = l.getNode(c.MatchAniPool.OnePropUseAniPrefab);
        o.parent = this.effectParent;
        o.setPosition(this.getV3Pos(e.row, e.col));
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = t;
        i.premultipliedAlpha = false;
        i.setCompleteListener(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(c.MatchAniPool.OnePropUseAniPrefab, o);
            a && a();
        });
        return i;
    };
    public playOnePropUseBoomAni(e, t, a) {
        var o;
        if (e == c.PropType.Propeller) {
            (o = this.createOnePropUseItem(t, this.propellerFlySpData, a)).premultipliedAlpha = true;
            o.setAnimation(0, "Remove", false);
        } else {
            if (e == c.PropType.LightingBall) return;
            if (e == c.PropType.RocketHorzontall) {
                (o = this.createOnePropUseItem(t, this.rocketUseSpData, a)).node.angle = 90;
                o.setAnimation(0, "Remove", false);
            } else e == c.PropType.RocketVertial ? (o = this.createOnePropUseItem(t, this.rocketUseSpData, a)).setAnimation(0, "Remove", false) : e == c.PropType.Tnt && (o = this.createOnePropUseItem(t, this.tntBoomSpData, a)).setAnimation(0, "Blast", false);
        }
    };
    public playPropellerDorpAni(e, t) {
        var a = this.createOnePropUseItem(e, this.propellerDropSpData, t);
        a.node.angle = 0;
        a.premultipliedAlpha = false;
        a.setAnimation(0, "Blast", false);
    };
    public playLightingAni(e, t, a) {
        var o = this.createOnePropUseItem(e, this.lightingSpData, null);
        o.setCompleteListener(function () {
            o.setCompleteListener(null);
        });
        var i = o.node, r = this.getV3Pos(t.row, t.col), n = cc.v2(i.position).sub(cc.v2(r)), u = MathUtils.getTwoPointsRadian2(i.position, r);
        i.scaleY = 0;
        i.angle = 180 - u;
        o.setAnimation(0, "Remove", false);
        cc.tween(i).to(.3, {
            scaleY: n.mag() / 75
        }).delay(.2).call(function () {
            o.skeletonData = null;
            l.putNode(c.MatchAniPool.OnePropUseAniPrefab, i);
            a && a();
        }).start();
    };
    public playLightingsBallAni(e, t) {
        var a = this.createDoublePropItem(e, this.doubleLightBallSpData, null), o = a.setAnimation(0, "Blast", false);
        a.setTrackEventListener(o, function (e, a) {
            "baopo" == a.data.name && t && t();
        });
    };
    public playLightBallUseAni(e) {
        var t = this.createOnePropUseItem(e, this.lightBallUseSpData, null);
        t.node.angle = 0;
        t.premultipliedAlpha = false;
        t.setCompleteListener(function () {
            t.setCompleteListener(null);
        });
        t.setAnimation(0, "whirl", true);
        return t;
    };
    public playLightBallBoomAni(e, t) {
        e.node.angle = 0;
        e.premultipliedAlpha = false;
        e.setCompleteListener(function () {
            e.setCompleteListener(null);
            t && t();
        });
        e.setAnimation(0, "blast", false);
    };
    public recoverLightBallUseAni(e) {
        if (e.getComponent(sp.Skeleton).skeletonData == this.lightBallUseSpData) {
            e.getComponent(sp.Skeleton).skeletonData = null;
            l.putNode(c.MatchAniPool.OnePropUseAniPrefab, e);
        }
    };
    public playEndStepAni(e, t, a) {
        u.playEndStepAudio1();
        var o = this.createOnePropUseItem(e, this.lightingSpData, null);
        o.setCompleteListener(function () {
            o.setCompleteListener(null);
        });
        var i = o.node, r = this.getV3Pos(t.row, t.col), n = cc.v2(i.position).sub(cc.v2(r)), p = MathUtils.getTwoPointsRadian2(i.position, r);
        i.scaleY = 0;
        i.angle = 180 - p;
        o.setAnimation(0, "Remove", false);
        cc.tween(i).to(.3, {
            scaleY: n.mag() / 75
        }).delay(.2).call(function () {
            o.skeletonData = null;
            l.putNode(c.MatchAniPool.OnePropUseAniPrefab, i);
            a && a();
            u.playEndStepAudio2();
        }).start();
    };
    public loadDoublePropSpData() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.loadDir("spine/doubleProps", sp.SkeletonData, function (o, i) {
                if (o) {
                    n.error("加载多个道具使用动画数据报错", o);
                    a();
                }
                i.forEach(function (t) {
                    switch (t.name) {
                        case "Double_HuoYaoTong":
                            e.doubleTntSpData = t;
                            break;

                        case "Double_QiSeQiu":
                            e.doubleLightBallSpData = t;
                            break;

                        case "HuoYaoTong-HuoJian":
                            e.tntAndRocketSpData = t;
                            break;

                        case "TuoLuo-HuoJian":
                            e.propllerAndRocketSpData = t;
                            break;

                        case "TuoLuo-HuoYaoTong":
                            e.propllerAndTntSpData = t;
                    }
                });
                e.doubleTntSpData && e.doubleLightBallSpData && e.tntAndRocketSpData && e.propllerAndRocketSpData && e.propllerAndTntSpData ? t(null) : a();
            });
        });
    };
    public loadDoublePropPrefab() {
        var e = this;
        return new Promise(function (t, a) {
            cc.resources.load("prefabs/doublePropPrefab", cc.Prefab, function (o, i) {
                if (o) {
                    n.error("未找到多个道具使用动画预制体", o);
                    a();
                }
                e.onePropUsePrefab = i;
                l.initPool(e.onePropUsePrefab, 0, c.MatchAniPool.DoublePropAniPrefab);
                t(i);
            });
        });
    };
    public createDoublePropItem(e, t, a) {
        var o = l.getNode(c.MatchAniPool.DoublePropAniPrefab);
        o.parent = this.effectParent;
        o.setPosition(this.getV3Pos(e.row, e.col));
        var i = o.getComponent(sp.Skeleton);
        o.angle = 0;
        i.skeletonData = t;
        i.premultipliedAlpha = false;
        i.setCompleteListener(function () {
            i.setCompleteListener(null);
            i.skeletonData = null;
            l.putNode(c.MatchAniPool.DoublePropAniPrefab, o);
            a && a();
        });
        return i;
    };
    public playDoubleTntAni(e, t) {
        this.createDoublePropItem(e, this.doubleTntSpData, t).setAnimation(0, "Blast", false);
    };
    public playTntAndRocketAni(e, t, a, o, i) {
        var r = this.createDoublePropItem(e, this.tntAndRocketSpData, i), l = "";
        if (a == c.PropType.Tnt) if (o == c.PropType.RocketHorzontall) l = e.row !== t.row ? "turn_ZHH_2" : "turn_ZHH_1"; else {
            if (o != c.PropType.RocketVertial) {
                n.log("炸药-火箭动画传参错误-----");
                return;
            }
            l = e.row !== t.row ? "turn_ZHS_2" : "turn_ZHS_1";
        } else if (o == c.PropType.Tnt) if (a == c.PropType.RocketHorzontall) l = e.row !== t.row ? "turn_ZHH_2" : "turn_ZHH_1"; else {
            if (a != c.PropType.RocketVertial) {
                n.log("炸药-火箭动画传参错误-----");
                return;
            }
            l = e.row !== t.row ? "turn_ZHS_2" : "turn_ZHS_1";
        }
        n.log("炸药-火箭---", e, t, a, o, l);
        r.setAnimation(0, "" + l, false);
    };
    public playFlyPropllerAni(e, t, a) {
        t.skeletonData = null;
        if (a == c.PropType.RocketHorzontall) {
            t.skeletonData = this.propllerAndRocketSpData;
            t.setAnimation(0, "turn_TH", true);
        } else if (a == c.PropType.RocketVertial) {
            t.skeletonData = this.propllerAndRocketSpData;
            t.setAnimation(0, "turn_TS", true);
        } else if (a == c.PropType.Tnt) {
            t.skeletonData = this.propllerAndTntSpData;
            t.setAnimation(0, "turn_TH", true);
        } else {
            t.skeletonData = this.propllerOneSpData;
            t.setAnimation(0, "Rotate", true);
        }
    };
    public getV3Pos(e, t) {
        return cc.v3(e * MatchGameConfig.ITEM_WIDTH , -t * MatchGameConfig.ITEM_HEIGHT);
    };
}

export default PropAniCtrl.getInstance();